from sql_helper import MySqlHelper
import requests
from pyquery import PyQuery as pq
import json

def read_list(url,all_list=None):
    if all_list is None:
        all_list = []
        if requests.get(url=url).status_code == 200:
            response = json.loads(requests.get(url=url).text)
            for i in response['subjects']:
                l = []
                l.append(i['rate'])
                l.append(i['title'])
                l.append(i['cover'])
                all_list.append(l)
            print('ok')
    return all_list



if __name__ == '__main__':
    url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=100&page_start=0"
    new_list = read_list(url)
    mysql = MySqlHelper()
    sql = "INSERT INTO movies(rate, title, cover) VALUES(%s,%s,%s)"
    mysql.exec_many(sql, new_list)